package org.vectrics.recruiter.struts.tag;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ecs.html.A;
import org.apache.log4j.Category;
import org.vectrics.DataException;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.user.UserEmployment;
import org.vectrics.struts.VectricsTagSupport;
import org.vectrics.user.Person;

public class CandidateSummaryTag extends VectricsTagSupport {
	public static final long serialVersionUID = 55533151;
    private static Category log = Category.getInstance(CandidateSummaryTag.class);
    private SimpleDateFormat jobDateFormat = new SimpleDateFormat("MM-yyyy");
    private static int SECTION_WIDTH = 400;
    public static String EDIT_USER_HREF = "/profile/edit.do";
    
	protected String getEditUserHref(HttpServletRequest request) {
		return(request.getContextPath() + EDIT_USER_HREF);        
	}

	protected String getNewUserHref(HttpServletRequest request) {
		return(null);
	}    
    
    protected Person getAuthUser(HttpServletRequest request, HttpSession session) {
        log.debug("getUserProfile() called");
        
        Person user = null;
        try {
	        user = this.getAuthPerson();
			try {
				if (user != null) {
					user = CoreServiceLocator.getPersonService().findPersonById(user.getId());
					log.debug("finding user contact info");
					
					user.createContactInfoIfNotExist();
				}
			}
			catch (DataException de) {
				log.error("doStartTag() - DataException finding user: " + user.getId());
			}
        }
        catch (Exception ex) {
        	log.error("Exception get authenticated user", ex);
        }
        return(user);
    }
    


	public String makeStartTag()  {
        log.debug("doRender() called");

		HttpServletRequest request = (HttpServletRequest)super.pageContext.getRequest();
		
        Table mainTable = new Table();
        Row mainRow = mainTable.addRow();
        Cell mainCell = mainRow.addCell();
        
        try {
	        log.debug("doRender() - getting user profile");
	        Person person = this.getAuthPerson();
	        
	        if (person == null) {
	            log.error("doRender() - user profile not found for request.");
	            mainCell.addElement("Could not find userProfile.");
	        }
	        else {
	            log.debug("doRender() - making candidate cell.");
	            makeCandidateCell(request, mainCell, person);
	        }
        }
        catch (Exception ex) {
        	log.error("doStartTag() - exception ", ex);
        }
        
		return(mainTable.toString());
    }
    
    
   
    
    protected void makeCandidateCell(HttpServletRequest request, Cell mainCell, Person user) {
        log.debug("makeCandidateCell() - called");

		log.debug("makeCandidateCell() - setting up table for display");
		BorderTable empTable = new BorderTable();
		empTable.setWidth(SECTION_WIDTH);
		Row hdrRow = empTable.addRow();
		Cell hdrCell = hdrRow.addCell();
		hdrCell.setClass("tableHdrFont");
		hdrCell.addElement(TextHelper.getFont("stdFont", "<b>Candidate Information</b>"));
		mainCell.addElement(empTable);

		Row row = empTable.addRow();
		Cell contentCell = row.addCell();
        
		/*
		log.debug("makeCandidateCell() - displaying current resume information");
		String resumeText = null;
		try {
			log.debug("makeCandidateCell() - getting resume");
			resumeText = RecruitUserManager.getResumeText(user);
			if (log.isDebugEnabled()) {
				log.debug("makeCandidateCell() - resumeText = " + resumeText);
			}
		}
		catch (DataException de) {
			log.error("makeCandidateCell() - DataException getting resume", de);
		}
		
		contentCell.addElement("<br>");
		if ((resumeText != null) && (resumeText.length() > 0)) {
			contentCell.addElement(TextHelper.getFont("subFont", "Text Resume on File"));
		}
		else {
			contentCell.addElement(TextHelper.getFont("subFont", "Text Resume Missing"));
		}
		contentCell.addElement("<br>");
		*/

		log.debug("makeCandidateCell() - retrieving employment list");
        Collection employmentList = null;
        try {
        	employmentList = RecruitServiceLocator.getRecruitUserService().getEmployments(user);
        
			Iterator iter = employmentList.iterator();
        
        
			log.debug("makeCandidateCell() - displaying employment list");
			if (iter.hasNext()) {
				Table innerTable = new Table();
				row = innerTable.addRow();
				Cell cell = row.addCell();
				cell.addElement(TextHelper.getFont("subFont", "<b>Title</b>"));
				cell = row.addCell();
				cell.addElement(TextHelper.getFont("subFont", "<b>Date</b>"));
				while (iter.hasNext()) {
					UserEmployment emp = (UserEmployment)iter.next();
					log.debug("makeCandidateCell() - displaying an employment entry");
					row = innerTable.addRow();
					cell.addElement(TextHelper.getFont("subFont", emp.getTitle()));
					cell = row.addCell();
					cell.addElement(TextHelper.getFont("subFont", emp.getCompanyName()));
                
					String startDateStr = "?";
					String endDateStr = "Present";
					if (emp.getStartDate() != null) {
						startDateStr = jobDateFormat.format(emp.getStartDate());
					}
					if (emp.getEndDate() != null) {
						endDateStr = jobDateFormat.format(emp.getEndDate());
					}
                
					cell = row.addCell();
					cell.addElement(TextHelper.getFont("subFont", startDateStr));
                
					cell = row.addCell();
					cell.addElement(TextHelper.getFont("subFont", endDateStr));
				}
			}
			else {
				log.debug("makeCandidateCell() - no employment entries found for user: " + user.getUser().getUserName());
				//contentCell.addElement(TextHelper.getFont("subFont", "<br><i>You have not entered your employment history.</i>"));
			}
			contentCell.addElement("<br><br>");
        	
        }
        catch (org.vectrics.DataException de) {
        	log.error("DataException in makeCandidateCell", de);
        }

		log.debug("makeCandidateCell() - making edit link");
        A link = new A();
        link.setHref(request.getContextPath() + "/profile/edit_resume.do");
        link.setClass("subLink");
        link.addElement("View / Change Resume");
        contentCell.addElement(link);
        
    }
    
}